package com.github.binarywang.demo.wx.mp.service;

import com.alibaba.fastjson.JSON;
import com.github.binarywang.demo.wx.mp.beans.FocusInfo;
import com.github.binarywang.demo.wx.mp.beans.FocusParam;
import com.github.binarywang.demo.wx.mp.beans.MeiPaParam;
import com.github.binarywang.demo.wx.mp.utils.FocusUtils;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.mp.api.WxMpService;
import me.chanjar.weixin.mp.bean.message.WxMpXmlMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutMessage;
import me.chanjar.weixin.mp.bean.message.WxMpXmlOutNewsMessage;
import me.chanjar.weixin.mp.bean.result.WxMpUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 关注推送服务
 *
 * @author xiaoban
 * @date 2021/9/3 下午12:51
 */
@Service
@Slf4j
public class PushService {
    @Autowired
    private LsWxMpUserService wxMpService;

    @Autowired
    private FocusInfoService focusInfoService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private wxservice wx;

    public WxMpXmlOutMessage getMessage(WxMpUser userWxInfo, WxMpXmlMessage wxMessage, WxMpService weixinService) {

        String eventKey = wxMessage.getEventKey();
        if (eventKey==null){
            return null;
        }
        if (!eventKey.contains("LS_FOCUS")){
            return null;
        }
        eventKey = eventKey.replace("qrscene_","");
        FocusParam param = JSON.parseObject(eventKey,FocusParam.class);
        if (param == null) {
            log.error("关注参数解析错误!");
            return null;
        }
        String mcId = param.getMc();
        String appId =wx.getAppIdbyclientId(param.getClient_id());


        log.info("商户ID {}",mcId);
        if (userWxInfo == null) {
            log.error("从微信官方没拿到数据");
            return null;
        }
        com.github.binarywang.demo.wx.mp.beans.WxMpUser mpUser = com.github.binarywang.demo.wx.mp.beans.WxMpUser.basicInfo(userWxInfo);

        Boolean ok = wxMpService.addOrUpdate(mpUser);//保存用户关注

        com.github.binarywang.demo.wx.mp.beans.WxMpUser mp = wxMpService.getByLikeOpenId(mpUser.getOpenId());

        log.info("更新微信用户 {} {}",ok,mp);

        //开始拿号
        FocusInfo focusInfo = focusInfoService.getByAppIdAndOpenId(mpUser.getOpenId(), appId);
        if (focusInfo!=null){
            log.info("关注过了 {}",focusInfo);
            return null;
        }
        focusInfo = new FocusInfo();
        focusInfo.setAppId(appId);
        focusInfo.setOpenId(mpUser.getOpenId());
        focusInfo.setWxMpUser(mpUser);
        //保存数据库
        ok = focusInfoService.add(focusInfo);
        log.info("保存关注历史 -> {}",ok);
        String key = FocusUtils.getPreKey(mpUser);
        log.info("查询KEY {}",key);
        Object obj = redisService.getObj(key);
        log.info("redis拿到的 {}",obj);
        MeiPaParam meiPaParam = redisService.getObj(key);
        if (meiPaParam==null){
            log.warn("不存在此订单");
            return null;
        }

        WxMpXmlOutNewsMessage.Item item = new WxMpXmlOutNewsMessage.Item();
//        item.setUrl(meiPaParam.getCallback());
        item.setUrl("");
        item.setTitle("点我领取环保袋");
        item.setDescription("领取环保袋!\n本链接 1 次有效!");
        item.setPicUrl("https://lssh.oss-cn-shenzhen.aliyuncs.com/static/web/icon/cr.png");
        log.info("返回出袋链接..." + meiPaParam.getCallback());
        return  WxMpXmlOutMessage.NEWS()
            .addArticle(item)
            .toUser(wxMessage.getFromUser())
            .fromUser(wxMessage.getToUser())
            .toUser(wxMessage.getFromUser())
            .build();

    }




}
